unit GMedParkPajamInIrasai;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, GFixForm, bendri, Sandelys_dm, StdCtrls, Mask,
  DBCtrls, Buttons, ActnList,
  ExtCtrls, cxLookAndFeelPainters, cxButtons, cxTextEdit,
  cxDBEdit, cxDropDownEdit, cxLookupEdit, cxDBLookupEdit,
  cxDBLookupComboBox, cxControls, cxContainer, cxEdit, cxMaskEdit, cxCalc,
  DB, cxPropertiesStore, Menus, cxGraphics, cxLabel, cxCheckBox;

type
  TfMedParkPajamInIrasai = class(TGamaFixForm)
    Label5: TLabel;
    eKiekis: TcxDBCalcEdit;
    ePrekesID: TcxDBTextEdit;
    sMode: TStaticText;
    ActionList1: TActionList;
    aPaimtiPreke: TAction;
    pOK: TPanel;
    bCancel: TcxButton;
    bOK: TcxButton;
    Label16: TLabel;
    eLikutis: TcxTextEdit;
    DS: TDataSource;
    Label1: TLabel;
    GamaPropertiesStore: TcxPropertiesStore;
    cxLabel1: TcxLabel;
    eKodas: TcxDBTextEdit;
    ePNr: TcxDBTextEdit;
    eRusis: TcxDBTextEdit;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    eStoris: TcxDBCalcEdit;
    ePlotis: TcxDBCalcEdit;
    Label6: TLabel;
    ePakNr: TcxDBTextEdit;
    Label7: TLabel;
    eIlgis: TcxDBCalcEdit;
    ePrinted: TcxDBCheckBox;
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure eKiekisChange(Sender: TField);
    procedure eKiekisPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption;
      var Error: Boolean);
  private
    { Private declarations }
    PVM: double;
    procedure SetMode(v: string);
  public
    { Public declarations }
    procedure ClearMode;
    property Mode: string write SetMode;
  end;

var
  fMedParkPajamInIrasai: TfMedParkPajamInIrasai;

implementation

uses
  DBClient, Math, sandelys_conf, doktip,  uSandDokTip, med_dm,
  tools, JvJCLUtils;

{$R *.dfm}

procedure TfMedParkPajamInIrasai.FormShow(Sender: TObject);
begin
  DS.Enabled := true;

  ActiveControl := eIlgis;

  DS.DataSet.FieldByName('ilgis').OnChange := eKiekisChange;
  DS.DataSet.FieldByName('kiekis').OnChange := eKiekisChange;
  eKiekisChange(nil);
  //
  //ePrekesID.Properties.OnEditValueChanged := ePrekesIDChange;

  //eSN.Properties.OnEditValueChanged := eSNChange;
  //eSNChange(nil);

end;

procedure TfMedParkPajamInIrasai.eKiekisChange(Sender: TField);
var
  storis: Double;
  plotis: Double;
  ilgis: Double;
  kiekis: Double;
  turis: Double;
  plotas: Double;

  plotasViso: Double;
begin
  if (DS.DataSet.FieldByName('Tipas').AsString = 'R') then begin
    storis := DS.DataSet.FieldByName('storis').AsFloat;
    ilgis := DS.DataSet.FieldByName('ilgis').AsFloat;

    turis := VarToFloat(dmMed.tbMedRastKub.Lookup('galas;ilgis', VarArrayOf([storis, ilgis]), 'turis'));

    DS.DataSet['turis'] := turis;

  end;
  if (DS.DataSet.FieldByName('Tipas').AsString = 'M') then begin
    storis := DS.DataSet.FieldByName('storis').AsFloat;
    plotis := DS.DataSet.FieldByName('plotis').AsFloat;
    ilgis := DS.DataSet.FieldByName('ilgis').AsFloat;
    kiekis := DS.DataSet.FieldByName('kiekis').AsFloat;

    turis := storis*plotis*ilgis;
    
    DS.DataSet['turis'] := turis;
  end;
  if (DS.DataSet.FieldByName('Tipas').AsString = 'P') then begin
    storis := DS.DataSet.FieldByName('storis').AsFloat;
    plotis := DS.DataSet.FieldByName('plotis').AsFloat;
    ilgis := DS.DataSet.FieldByName('ilgis').AsFloat;
    kiekis := DS.DataSet.FieldByName('kiekis').AsFloat;

    plotas := plotis*ilgis/1000000.0;
    turis := plotas*(storis/1000);

    DS.DataSet['turis'] := turis;
    DS.DataSet['plotas'] := plotas;
  end;
  if (DS.DataSet.FieldByName('Tipas').AsString = 'G') then begin
    storis := DS.DataSet.FieldByName('storis').AsFloat;
    plotis := DS.DataSet.FieldByName('plotis').AsFloat;
    ilgis := DS.DataSet.FieldByName('ilgis').AsFloat;
    kiekis := DS.DataSet.FieldByName('kiekis').AsFloat;

    plotas := ilgis/1000.0;
    turis := storis*plotis*ilgis/1000000000.0;


    DS.DataSet['turis'] := turis;
    DS.DataSet['plotas'] := plotas;
  end;

  plotasViso := plotas*kiekis;

  eLikutis.Text := FormatFloat('0.0000',plotasViso);

end;

procedure TfMedParkPajamInIrasai.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  DS.DataSet.FieldByName('ilgis').OnChange := nil;
  DS.DataSet.FieldByName('kiekis').OnChange := nil;

  DS.Enabled := false;
end;

procedure TfMedParkPajamInIrasai.SetMode(v: string);
begin
  sMode.Caption := v;
end;

procedure TfMedParkPajamInIrasai.ClearMode;
begin
  sMode.Caption := '';
end;

procedure TfMedParkPajamInIrasai.FormCreate(Sender: TObject);
begin
  DS.Enabled := false;

end;

procedure TfMedParkPajamInIrasai.eKiekisPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
begin
  if DisplayValue <> '' then
  DisplayValue := Rounding(DisplayValue, dmSandelys.SandConfigParam.KiekioTikslumas);
end;

end.
